﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.Base;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.FinanceManage;
using SmartWMS.Server.FinanceManage.PayTypeServices.Dto;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.FinanceManage.PayTypeServices
{
    public class PayTypeService : IPayTypeService
    {
        private readonly IRepository<PayType> _payTypeRepository;

        public PayTypeService(IRepository<PayType> payTypeRepository)
        {
            _payTypeRepository = payTypeRepository;
        }

        /// <summary>
        /// 财务类别添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> AddPayType(PayTypeDto dto)
        {
            dto.CreateTime = DateTime.Now;
            var payType = dto.MapTo<PayType>();

            var addlist = await _payTypeRepository.InsertAsync(payType);

            return addlist > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(false);
        }

        /// <summary>
        /// 财务类别删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeletePayType(PayTypeDto dto)
        {
            var payType = dto.MapTo<Category>();
            var result = await _payTypeRepository.DeleteByIdAsync(payType);
            return result > 0 ? Response<bool>.Ok(true) : Response<bool>.Fail(result > 0);
        }

        /// <summary>
        /// 财务类别批量删除
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<bool>> DeletePayTypeList(PayTypeDto dto)
        {
            var sid = dto.DelId.Split(',');

            var list = new List<PayType>();

            foreach (var item in sid)
            {
                var plist = _payTypeRepository.GetListQuery().Where(t => t.Id == int.Parse(item)).ToList();
                list.AddRange(plist);
            }
            var result = await _payTypeRepository.DeleteAsync(list);
            return result > 0 ? Response<bool>.Ok(result > 0) : Response<bool>.Fail(result > 0);
        }

        /// <summary>
        /// 财务类别编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<Response<bool>> EditPayType(PayTypeDto dto)
        {
            var payType = dto.MapTo<PayType>();
            var result = _payTypeRepository.UpdateAsync(payType);
            return result.Result > 0 ? Task.FromResult(Response<bool>.Ok(result.Result > 0)) : Task.FromResult(Response<bool>.Fail(result.Result > 0));
        }

        /// <summary>
        /// 根据id获取财务类别
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<PayTypeDto>> GetPayTypeById(KeyEntity id)
        {
            var payType = await _payTypeRepository.GetEntityByIdAsync(id);
            var response = payType.MapTo<PayTypeDto>();
            return response != null ? Response<PayTypeDto>.Ok(response) : Response<PayTypeDto>.Fail("查询失败");
        }

        /// <summary>
        /// 财务类别列表
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Response<List<PayTypeDto>>> GetPayTypeList(string? name)
        {
            var payTypeList = await _payTypeRepository.GetAllListAsync();
            if (!string.IsNullOrEmpty(name))
            {
                payTypeList = payTypeList.Where(m => m.TypeName.Contains(name)).ToList();
            }
            var response = payTypeList.MapToList<PayTypeDto>();
            return response != null ? Response<List<PayTypeDto>>.Ok(response) : Response<List<PayTypeDto>>.Fail("查询失败");
        }
    }
}
